package jmine.tec.persist.schema.impl.elements.type;

import java.sql.Types;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import jmine.tec.persist.schema.impl.elements.ColumnTypeElement;

import org.hibernate.type.BooleanType;
import org.hibernate.type.Type;

/**
 * Strategy para lidar com campos boolean.
 * 
 * @author lundberg
 */
public class BooleanTypeStrategy implements TypeStrategy {

    private final Set<AllowedValue> allowedValues;

    /**
     * Construtor
     */
    public BooleanTypeStrategy() {
        AllowedValue[] values = new AllowedValue[]{ new IntegerAllowedValue(0, "FALSO"), new IntegerAllowedValue(1, "VERDADEIRO") };
        this.allowedValues = Collections.unmodifiableSet(new HashSet<AllowedValue>(Arrays.asList(values)));
    }

    /**
     * {@inheritDoc}
     */
    public List<String> checkUserDefinedValues(ColumnTypeElement columnType, Class<?> returnedClass) {
        return Collections.emptyList();
    }

    /**
     * {@inheritDoc}
     */
    public int correctSqlType(Type type, int current) {
        return Types.BIT;

    }

    /**
     * {@inheritDoc}
     */
    public Set<AllowedValue> getAllowedValues(Class<?> returnedClass) {
        return this.allowedValues;
    }

    /**
     * {@inheritDoc}
     */
    public boolean hasLimitedAllowedValues() {
        return true;
    }

    /**
     * {@inheritDoc}
     */
    public void inferTypeParameters(ColumnTypeElement columnType, Class<?> returnedClass) {
        return;
    }

    /**
     * {@inheritDoc}
     */
    public boolean matches(ColumnTypeElement columnTypeElement, Type type) {
        Class<?> returnedClass = type.getReturnedClass();
        boolean isBoolean = Boolean.class.isAssignableFrom(returnedClass) || boolean.class.isAssignableFrom(returnedClass);
        boolean isHibernateType = BooleanType.class.equals(type.getClass());
        return isBoolean && isHibernateType;
    }
}